/*
 * 
 */

package com.simplesoftwares.client.library.appskeleton;

import java.util.Vector;

import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.simplesoftwares.client.library.appstructure.ViewContainer;


/**
 * The Class AppMemory.Manages initialization of application.Its attributes contains reference of 
 * <b>current screen</b>
 * all the menu items,current screen state.
 * To Do: Singleton design pattern is for name only a good design challenge for komal,pradnya,sumit.No practical
 * benifits though.
 * @author Ajay, Pradnya
 * @version 1.0
 * 
 */
public abstract class AppMemory
{
	
	/** Vector of menu items contains reference of all menu items corresponding to all screens.
	 *  Get initialized by Concrete Implementation.
	 * */
	public  Vector<MenuItems>items;
	
	/** The Reference to outer holder of app */
	public AppContainer apl;
	
	/** Reference to current screen of application which user is working on particular instance */
	public  AbstractScreen currentScreen;
	
	/** Reference to current screen state of application which user is working on particular instance */
	public  ScreeenState currentState;
	
	/** Instance of class {@link AppSkeleton} This objects encapsulates refrences of Process Name ,AppLevelMenu
	 * etc.*/
	public AppSkeleton skeleton;
	
	/** The memory. Singleton object, only one instance will be created throughout an user session.
	 * 
	 * */
	protected static AppMemory memory;
	
	

	/**
	 * Instantiates a new app memory.
	 */
	protected AppMemory() 
	{
		items=new Vector<MenuItems>();
	}
	
	/**
	 * Removes the panel from {@link AppSkeleton} and stick new panel
	 *
	 * @param mv the mv
	 */
	public void stickPnel(ViewContainer mv)
	{
		if(this.apl.getWidgetCount()==4)
			apl.remove(3);
		apl.add(mv.content);
	}
	
	/**
	 * Change current screen,and sets the process name
	 *
	 * @param processname the processname to be applied
	 * @param screen Screen which will become current screen
	 */
	public  void setCurrentScreenandChangeProcessName(String processname,AbstractScreen screen)
	   {
		 this.currentScreen=screen;
		 skeleton.getProcessName().setText(processname); 
	   }
	
	/**
	 * Method creates the {@link AppContainer} and stick that on {@link RootPanel}
	 */
	private void setAppContainer()
	{
		RootLayoutPanel rp = RootLayoutPanel.get();
		rp.clear();
		rp.add(this.apl);	
    }
	
	/**
	 * Sets the list.
	 *
	 * @param item the item
	 * @param lb the lb
	 */
	public static void setList(String item,ListBox lb)
	{
		int count = lb.getItemCount();
		for(int i=0;i<count;i++)
		{
			if(item.trim().equals(lb.getItemText(i).trim()))
			{
				lb.setSelectedIndex(i);
				break;
			}
		}
	}

	/**
	 * Gets the app memory.
	 *
	 * @return the app memory
	 */
	public static AppMemory getAppMemory() 
	{
		return memory;
		
	}
	
	
	/**
	 * Creates the Application outer structure , also initalizes the {@link AppContainer} variable apl.
	 * In this  navigationbarPanel,SideBarPanel and headerbar panel are created.
	 */
	private void createSkeleton()
	{
		//Implementation provided by Concrete class the method returns vector of menu items for 
		//navigational panel
		Vector<MenuItems> navimenus=createNavigationBar();
		//Implementat
		Vector<SideBarItems> sidemenus=createSideBar();
		String[] appmenus=getApplevelMenus();
		skeleton=new AppSkeleton(5, 17, 15);
		skeleton.createNavigationPanel(navimenus);
		skeleton.createSideBarPanel(sidemenus);
		skeleton.createAppHeaderBarPanel(appmenus);
		
		// Application Skeleton Initializes
		apl=new AppContainer(skeleton);
		
	}
	
	/**
	 * Utility method which creates and stick application skeleton
	 */
	public void initialingapp()
	{
		createSkeleton();
		setAppContainer();
	}

	
	/**
	 * Creates the navigation bar.
	 *
	 * @return the vector
	 */
	protected abstract Vector<MenuItems> createNavigationBar();
	
	/**
	 * Creates the side bar.
	 *
	 * @return the vector
	 */
	protected abstract Vector<SideBarItems> createSideBar();
	
	/**
	 * Gets the applevel menus.
	 *
	 * @return the applevel menus
	 */
	protected abstract String[] getApplevelMenus();
	
    /**
     * Go to home.
     */
    public abstract void goToHome() ;

	
	
	
}
